<!--
 * @Author: 梁卓
 * @Date: 2022-02-11 23:37:10
 * @LastEditTime: 2022-03-09 20:26:59
 * @LastEditors: Please set LastEditors
 * @Description: 阅读推文所得积分echarts展示
 * @FilePath: \dreamUi\src\componets\echarts\StudyScore.vue
-->
<template>
  <div class="study_page">
    <div style="width: 100%;height: 100%;" ref="study_echarts"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
export default {
  name: 'StudyScore',
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.getData()
    window.addEventListener('resize',this.chart)
  },
  destroyed() {
    window.removeEventListener('resize',this.chart);
  },
  methods: {
    getData() {
      this.$ajax.get('/readScore/getNum').then(res => {
        const {data} = res.data
        var score1 = 0;var score2 = 0;var score3 = 0;var score4 = 0;var score5 = 0
        data.forEach(item => {
          if (item.date >= 0 && item.date < 5) {
            score1 += item.count;
          } else if (item.date >= 0 && item.date < 5) {
            score2 += item.count;
          } else if (item.date >= 5 && item.date < 10) {
            score3 += item.count;
          } else if (item.date >= 10 && item.date < 15) {
            score4 += item.count;
          } else if (item.date >= 15 && item.date < 20) {
            score5 += item.count;
          }
        });
        this.myEcharts(score1,score2,score3,score4,score5)
      });
    },
    myEcharts(score1,score2,score3,score4,score5) {
      this.chart = echarts.init(this.$refs.study_echarts);
      const size = this.$refs.study_echarts.offsetWidth;
      //配置图表
      this.chart.setOption({
        title: {
          text: '各个学习积分分段的学生人数'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '8%',
          left: 'center'
        },
        series: [
          {
            name: '学习积分',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '40',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              { value: score1, name: '0 ~ 5' },
              { value: score2, name: '5 ~ 10' },
              { value: score3, name: '10 ~ 15' },
              { value: score4, name: '15 ~ 20' },
              { value: score5, name: '20 ~ 25' }
            ]
          }
        ]
      });
      this.chart.resize();
    }
  },
};
</script>

<style scoped>
.study_page{
  width: 100%;
  height: 100%;
}
</style>